Skip to content

about smart pointers and types such as unique ptr , shared ptr and weak ptr#17

Open
Butcher3Years wants to merge 1 commit intoObject-Lifetime-(stack-$-scope-lifetimes)from
Smart-pointers----Its-Types
Open

about smart pointers and types such as unique ptr , shared ptr and weak ptr#17
Butcher3Years wants to merge 1 commit intoObject-Lifetime-(stack-$-scope-lifetimes)from
Smart-pointers----Its-Types

Conversation

@Butcher3Years
Copy link
Copy Markdown
Owner

Smart Pointers in C++ – unique_ptr, shared_ptr, weak_ptr (2026)

Smart pointers are RAII classes that automatically manage heap-allocated objects.
They eliminate manual delete, prevent most leaks/dangling pointers, and make your code safe & expressive.

Cherno's rule:
"Raw new and delete in 2026? You're doing it wrong. Smart pointers are the only way."

The Three Main Smart Pointers

Smart Pointer Ownership Model Auto Delete When? Copyable? Ref Counting? Best For / When to Use It Overhead
std::unique_ptr<T> Exclusive (single owner) When pointer goes out of scope or moved No (move only) No Default choice for heap objects. Clear single owner, zero overhead, fast. Almost zero
std::shared_ptr<T> Shared (multiple owners) When last shared_ptr dies (ref count → 0) Yes Yes (atomic) When multiple parts of code need to share the same object (scene graph, resources). Small (ref count)
std::weak_ptr<T> Non-owning observer Never (doesn't own) Yes Yes (observe) Break cycles, cache references, check if shared_ptr still alive without owning it. Small

All live in <memory> header.

1. std::unique_ptr<T> – Exclusive Ownership (Your Go-To)

  • Only one unique_ptr can own the object at a time
  • When it dies or is moved → object is automatically deleted
  • No reference counting → zero overhead (usually same as raw pointer)
#include <memory>
#include <iostream>

class Entity {
public:
    Entity()          { std::cout << "Entity created\n"; }
    ~Entity()         { std::cout << "Entity destroyed\n"; }
};

int main() {
    // Modern & safe way (C++14+)
    auto ptr = std::make_unique<Entity>();      // constructor runs

    // Use like raw pointer
    // ptr->DoSomething();

    // Transfer ownership
    auto ptr2 = std::move(ptr);                 // ptr now nullptr

}   // ptr2 dies → "Entity destroyed" printed automatically

What is Reference Counting 

Imagine an object (like an Entity) is a toy.

Every time someone wants to "use" the toy, they take a ticket (a shared_ptr).
Every ticket increases a counter by 1 ("Hey, one more person is holding this toy").
When someone is done with the toy, they throw away their ticket → counter decreases by 1.
When the last ticket is thrown away (counter reaches 0), the toy is automatically destroyed (deleted).

That counter is the reference count.


How shared_ptr Does It

#include <memory>
#include <iostream>

class Entity {
public:
    ~Entity() { std::cout << "Entity destroyed\n"; }
};

int main() {
    std::cout << "Start\n";

    auto s1 = std::make_shared<Entity>();          // ref count = 1

    {
        auto s2 = s1;                              // ref count = 2
        auto s3 = s2;                              // ref count = 3

        std::cout << "Inside block – ref count: " << s1.use_count() << "\n";
    }                                              // s2 & s3 die → ref count = 1

    std::cout << "After block – ref count: " << s1.use_count() << "\n";

}  // s1 dies → ref count = 0 → "Entity destroyed" printed automatically


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant